package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.*;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.SaleAccountMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IProductStockService;
import cn.wolfcode.wms.web.exception.LogicalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
public class ProductStockServiceImpl implements IProductStockService {
    @Autowired
    private ProductStockMapper stockMapper;
    @Autowired
    private SaleAccountMapper saleAccountMapper;
    public PageResult query(QueryObject qo) {
        int count = stockMapper.queryForCount(qo);
        if(count == 0){
            return PageResult.empty(qo.getPageSize());
        }
        return new PageResult(stockMapper.queryForList(qo),count,qo.getCurrentPage(),qo.getPageSize());
    }

    public void stockIncome(StockIncomeBill bill) {
        for (StockIncomeBillItem item : bill.getItems()) {
            ProductStock productStock = stockMapper.selectProductStockByDepotIDAndProductId(bill.getDepot().getId(), item.getProduct().getId());
            if (productStock == null) {
                //1.库存当中没有该商品
                //创建并为该条库存设置相关信息
                ProductStock ps = new ProductStock();
                ps.setAmount(item.getAmount());
                ps.setDepot(bill.getDepot());
                ps.setPrice(item.getCostPrice());
                ps.setProduct(item.getProduct());
                ps.setStoreNumber(item.getNumber());
                //保存库存
                stockMapper.insert(ps);
            } else {
                //2.库存当有已经有该商品
                //计算价格(总的价格/总的数量)
                BigDecimal amount = productStock.getAmount().add(item.getAmount());
                BigDecimal number = productStock.getStoreNumber().add(item.getNumber());
                BigDecimal price = amount.divide(number, 2, RoundingMode.HALF_UP);
                productStock.setPrice(price);
                productStock.setStoreNumber(number);
                productStock.setAmount(amount);
                stockMapper.updateByPrimaryKey(productStock);
            }
        }
    }

    public void stockOutcome(StockOutcomeBill bill) {
        for (StockOutcomeBillItem item : bill.getItems()) {
            //查询出数据库中对应的明细
            ProductStock stock = stockMapper.selectProductStockByDepotIDAndProductId(bill.getDepot().getId(), item.getProduct().getId());
            //如果该库存不存在,则抛出异常
            if (stock == null) {
                throw new LogicalException("[" + bill.getDepot().getName() + "]仓库没有[" + item.getProduct().getName() + "]商品");
            }
            //如果有但是数量不足
            if (item.getNumber().compareTo(stock.getStoreNumber()) > 0) {
                throw new LogicalException("[" + bill.getDepot().getName() + "]仓库需要[" + item.getNumber() + "]个"
                        + item.getProduct().getName() + "商品,但实际只有[" + stock.getStoreNumber() + "]个");
            }
            //有且数量充足
            stock.setStoreNumber(stock.getStoreNumber().subtract(item.getNumber()).setScale(2, RoundingMode.HALF_UP));
            stock.setAmount(stock.getStoreNumber().multiply(stock.getPrice()).setScale(2, RoundingMode.HALF_UP));
            //更新订单
            stockMapper.updateByPrimaryKey(stock);
            //记录销售流水账
            SaleAccount sc = new SaleAccount();
            sc.setClientId(bill.getClient().getId());
            sc.setCostPrice(stock.getPrice());
            sc.setCostAmount(stock.getAmount());
            sc.setSalePrice(item.getSalePrice());
            sc.setSaleAmount(item.getAmount());
            sc.setNumber(item.getNumber());
            sc.setProductId(item.getProduct().getId());
            sc.setSaleManId(bill.getInputUser().getId());
            sc.setVdate(bill.getVdate());
            saleAccountMapper.insert(sc);
        }
    }
}
